package com.javen.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import com.javan.util.ObjtoLayJson;
import com.javen.controllerHelp.ControllerHelp;
import com.javen.model.CarInfo;
import com.javen.model.Page;
import com.javen.service.CarInfoService;

@Controller
@RequestMapping("/manage")
public class CarInfoController {
	
	@Autowired
	private CarInfoService carInfoService;
	//
	//添加车辆信息
	@RequestMapping(value="/insert", method=RequestMethod.GET,produces = "text/plain;charset=utf-8")  
	@ResponseBody
	public String insert(HttpServletRequest request) {
		String carOwner = request.getParameter("carOwner");
		String model = request.getParameter("model");
		String mileage = request.getParameter("mileage");
		String engineCapacity = request.getParameter("engineCapacity");
		String productiveDate = request.getParameter("productiveDate");
		String purchaseDate = request.getParameter("purchaseDate");
		//数据校验
//		try {
//			ControllerHelp.check1(carOwner);
//		} catch (Exception e) {
//			// TODO: handle exception
//			return e.getMessage();
//		}
		
		CarInfo carInfo = new CarInfo();
		carInfo.setCarOwner(carOwner);
		carInfo.setModel(model);
		carInfo.setMileage(mileage);
		carInfo.setEngineCapacity(engineCapacity);
		carInfo.setProductiveDate(productiveDate);
		carInfo.setPurchaseDate(purchaseDate);
		int status = carInfoService.insert(carInfo);
		String data = "{\"status\":\""+ status +"\",\"msg\":\"插入成功\"}"; 
		return data;
	}
//	public static void check1(String x) throws Exception {
//		try {
//			Integer.parseInt(x);
//		} catch (Exception e) {
//			// TODO: handle exception
//			throw new Exception("carOwner参数有问题");
//		}
//	}
	//分页查询所有车辆信息
	@RequestMapping(value="/selectAll", method=RequestMethod.GET,produces = "text/plain;charset=utf-8")  
	@ResponseBody
	public String selectAll(HttpServletRequest request) throws Exception {
		Page page = new Page();
		//接收page的limit和page offset
		String pageString = request.getParameter("page");
		Integer pageInteger = Integer.valueOf(pageString);
		String limitString = request.getParameter("limit");
		Integer limitInteger = Integer.valueOf(limitString);
		page.setPage(pageInteger-1);
		page.setLimit(limitInteger);
		page.setOffset((pageInteger-1)*limitInteger);
		List<CarInfo> carInfos = carInfoService.selectAll(page);
		String[] colums = {"id","carOwner","model","mileage","engineCapacity","productiveDate","purchaseDate"};
		String data = ObjtoLayJson.ListtoJson(carInfos, colums);
		System.out.println(data);
		return data;
	}
	//计算车辆信息数量
	@RequestMapping(value="/countAll", method=RequestMethod.GET,produces = "text/plain;charset=utf-8")  
	@ResponseBody
	public String countAll() {
		int count = carInfoService.countAll();
		return "{\"code\":\"200\",\"count\":\""+count+"\"}";
	}
	//分页查询车辆信息:根据车主
	@RequestMapping(value="/selectAllByCarOwner", method=RequestMethod.GET,produces = "text/plain;charset=utf-8")  
	@ResponseBody
	public String selectAllByCarOwner(HttpServletRequest request) throws Exception {
		Page page = new Page();
		//接收page的limit和page offset
		String pageString = request.getParameter("page");
		Integer pageInteger = Integer.valueOf(pageString);
		String limitString = request.getParameter("limit");
		Integer limitInteger = Integer.valueOf(limitString);
		String carOwner = request.getParameter("carOwner");
		page.setPage(pageInteger-1);
		page.setLimit(limitInteger);
		page.setOffset((pageInteger-1)*limitInteger);
		page.setCarOwner(carOwner);
		List<CarInfo> carInfos = carInfoService.selectAllByCarOwner(page);
		String[] colums = {"id","carOwner","model","mileage","engineCapacity","productiveDate","purchaseDate"};
		String data = ObjtoLayJson.ListtoJson(carInfos, colums);
		System.out.println(data);
		return data;

	}
	//根据车主计算车辆信息数量
	@RequestMapping(value="/countAllByCarOwner", method=RequestMethod.GET,produces = "text/plain;charset=utf-8")  
	@ResponseBody
	public String countAllByCarOwner(HttpServletRequest request) {
		String carOwner = request.getParameter("carOwner");
		int count = carInfoService.countAllByCarOwner(carOwner);
		return "{\"code\":\"200\",\"count\":\""+count+"\"}";
	}
//	//查询车辆信息:根据车主
//	@RequestMapping(value="/selectByUserName", method=RequestMethod.GET,produces = "text/plain;charset=utf-8")  
//	@ResponseBody
//	public String selectByUserName(HttpServletRequest request) throws Exception {
//		String carOwner = request.getParameter("carOwner");
//		List<CarInfo> carInfos = carInfoService.selectByUserName(carOwner);
//		String[] colums = {"id","carOwner","model","mileage","engineCapacity","productiveDate","purchaseDate"};
//		String data = ObjtoLayJson.ListtoJson(carInfos, colums);
//		System.out.println(data);
//
//		return data;
//
//	}
//	//查询车辆信息:根据id
//	@RequestMapping(value="/selectByPrimaryKey", method=RequestMethod.GET,produces = "text/plain;charset=utf-8")  
//	@ResponseBody
//	public CarInfo selectByPrimaryKey(HttpServletRequest request) {
//		String id = request.getParameter("id");
//		Integer idInteger = Integer.valueOf(id);
//		CarInfo carInfo = carInfoService.selectByPrimaryKey(idInteger);
//		return carInfo;
//	}
	//修改车辆信息
	@RequestMapping(value="/updateByPrimaryKey", method=RequestMethod.GET,produces = "text/plain;charset=utf-8")  
	@ResponseBody
	public String updateByPrimaryKey(HttpServletRequest request) {
		String id = request.getParameter("id");
		Integer idInteger = Integer.valueOf(id);
		String carOwner = request.getParameter("carOwner");
		String model = request.getParameter("model");
		String mileage = request.getParameter("mileage");
		String engineCapacity = request.getParameter("engineCapacity");
		String productiveDate = request.getParameter("productiveDate");
		String purchaseDate = request.getParameter("purchaseDate");
		CarInfo carInfo = new CarInfo();
		carInfo.setId(idInteger);
		carInfo.setCarOwner(carOwner);
		carInfo.setModel(model);
		carInfo.setMileage(mileage);
		carInfo.setEngineCapacity(engineCapacity);
		carInfo.setProductiveDate(productiveDate);
		carInfo.setPurchaseDate(purchaseDate);
		int status = carInfoService.updateByPrimaryKey(carInfo);
		String data = "{\"status\":\""+ status +"\",\"msg\":\"修改成功\"}"; 
		return data;
	}
	//用户删除
	@RequestMapping(value="/updateInfoByPrimaryKey", method=RequestMethod.GET,produces = "text/plain;charset=utf-8")  
	@ResponseBody
	//public String updateInfoByPrimaryKey(HttpServletRequest request) {
	public String updateInfoByPrimaryKey(int id) {
//		String id = request.getParameter("id");
//		Integer idInteger = Integer.valueOf(id);
		CarInfo carInfo = carInfoService.selectByPrimaryKey(id);
		System.out.println(carInfo.toString());
		carInfo.setCarOwner("无");
		carInfo.setPurchaseDate("无");
		System.out.println(carInfo.toString());
		System.out.println(carInfo.purchaseDate);
		int status = carInfoService.updateInfoByPrimaryKey(carInfo);
		String data = "{\"status\":\""+ status +"\",\"msg\":\"删除成功\"}"; 
		return data;
	}
	//管理员删除
	@RequestMapping(value="/deleteByPrimaryKey", method=RequestMethod.GET,produces = "text/plain;charset=utf-8")  
	@ResponseBody
	//public String deleteByPrimaryKey(HttpServletRequest request) {
	public String deleteByPrimaryKey(int id) {
		//String id = request.getParameter("id");
		//Integer idInteger = Integer.valueOf(id);
		//carInfoService.deleteByPrimaryKey(idInteger);
		System.out.println(id);
		int status = carInfoService.deleteByPrimaryKey(id);
		String data = "{\"status\":\""+ status +"\",\"msg\":\"删除成功\"}"; 
		return data;
	}
}
